package com.sh.service.user.impl;

import com.sh.common.PageData;
import com.sh.common.context.AppContextHolder;
import com.sh.feign.UserFeignClient;
import com.sh.pojo.domain.PageUserDomain;
import com.sh.pojo.domain.YunQiaoUserDomain;
import com.sh.pojo.vo.UserInfoVO;
import com.sh.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements IUserService {
    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public PageData<UserInfoVO> listUsers(Integer pageNum, Integer pageSize) {
        PageUserDomain sourceList = userFeignClient.listUsers(pageNum, pageSize);
        List<UserInfoVO> userInfoVOList = new ArrayList<>();
        if (sourceList.getResources() != null && !sourceList.getResources().isEmpty()) {
            sourceList.getResources().forEach(u -> {
                UserInfoVO vo = new UserInfoVO();
                vo.setYunQiaoUser(u);
                userInfoVOList.add(vo);
            });
        }
        return PageData.<UserInfoVO>builder().pageNum(sourceList.getStartIndex()).pageSize(sourceList.getItemsPerPage()).totalElements(sourceList.getTotalResults()).records(userInfoVOList).build();
    }

    @Override
    public UserInfoVO getUserInfo() {
        String userId = AppContextHolder.getUserId();
        YunQiaoUserDomain yunQiaoUser = userFeignClient.getUserInfo(userId);
        UserInfoVO vo = new UserInfoVO();
        vo.setYunQiaoUser(yunQiaoUser);
        return vo;
    }

    @Override
    public UserInfoVO getUserInfoById(String userId) {
        YunQiaoUserDomain yunQiaoUser = userFeignClient.getUserInfo(userId);
        UserInfoVO vo = new UserInfoVO();
        vo.setYunQiaoUser(yunQiaoUser);
        return vo;
    }
}
